package de.akabahn.ebd.control;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import de.akabahn.ebd.control.auth.AbstractAuthenticator;
import de.akabahn.ebd.control.auth.AccessEnum;
import de.akabahn.ebd.control.auth.RemoteAuthenticator;

/**
 * @author Peter Eimann
 */
public class EBD_ManageUserActivity extends EBD_DefaultUncaughtExceptionHandler {

	protected String userName;
	protected AccessEnum role;
	private AbstractAuthenticator authenticator;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        setContentView(R.layout.manageuser);
		userName = getIntent().getExtras().getString("de.akabahn.ebd.control.userName");
		role = AccessEnum.valueOf(getIntent().getExtras().getString("de.akabahn.ebd.control.roleName"));
		authenticator = RemoteAuthenticator.getInstance();

		Button change = (Button) findViewById(R.id.manageuser_change_button);
		change.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				manageUser();
			}
		});
	}

	@Override
	public void onStart() {
		super.onStart();

		initializeFields();
		TextView userInfo = (TextView)findViewById(R.id.manageuser_userInfo);
		userInfo.setText(getResources().getString(R.string.userInfo, userName, role));
	}

	private void manageUser() {
		String userName = ((EditText)findViewById(R.id.manageuser_username)).getEditableText().toString();
		String password = ((EditText)findViewById(R.id.manageuser_password)).getEditableText().toString();
		String role = ((Spinner)findViewById(R.id.manageuser_spinner_role)).getSelectedItem().toString();

		if (TextUtils.isEmpty(userName)) {
			Toast.makeText(EBD_ManageUserActivity.this,
					getResources().getString(R.string.manageuser_usernameEmpty), Toast.LENGTH_LONG).show();
		} else if (TextUtils.isEmpty(password)) {
			Toast.makeText(EBD_ManageUserActivity.this,
					getResources().getString(R.string.manageuser_passwordEmpty), Toast.LENGTH_LONG).show();
		} else if (role.startsWith("--")) {
			Toast.makeText(EBD_ManageUserActivity.this,
					getResources().getString(R.string.manageuser_noRole), Toast.LENGTH_LONG).show();
		} else if (!authenticator.manageUser(userName, password, AccessEnum.valueOf(role))) {
			Toast.makeText(EBD_ManageUserActivity.this,
					getResources().getString(R.string.manageuser_changeFailed), Toast.LENGTH_LONG).show();
		} else {
			Toast.makeText(EBD_ManageUserActivity.this,
					getResources().getString(R.string.manageuser_changeOk), Toast.LENGTH_SHORT).show();
			initializeFields();
		}
	}
	
	private void initializeFields() {
		((EditText) findViewById(R.id.manageuser_username)).setText("");
		((EditText) findViewById(R.id.manageuser_password)).setText("");
		((Spinner) findViewById(R.id.manageuser_spinner_role)).setSelection(0);
		((EditText) findViewById(R.id.manageuser_username)).requestFocus();
	}
}